<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use dh2y\qrcode\QRcode;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="产品列表")
 */
class Goods extends AdminController
{
    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Goods();
    }

      /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k => $v){
                //分类
                $classification = Db::name("classification")->where("id",$v["class_id"])->find();
                if(!empty($classification)){
                    $v['class_name'] = $classification['title'];
                }else{
                    $v['class_name'] = "";
                }
                $list[$k] = $v;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
 

    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('产品名称不能为空');
            }
            if(empty($post['class_id'])){
                $this->error('分类不能为空');
            }
            if(empty($post['url'])){
                $this->error('跳转链接不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'img'=>$post['img'],
                'url'=>$post['url'],
                'class_id'=>$post['class_id'],
                'remarks'=>$post['remarks'],
                'create_time'=>time()
            ];
            try {
               Db::name("goods")->insert($data);
               $gid = Db::name('goods')->getLastInsID();
               //生成二维码
              // $res = $this->generate_qrcode($gid,$data["url"]);
               $res = $this->generate_qrcode($gid,"https://gzhshop.clkejj.com/h5/#/pages/webview/webview?url=".$data["url"]."&gid=".$gid);
               if($res["code"] == 1){
                    Db::name("goods")->where("id",$gid)->update(["qrcode"=>$res["data"]]);
               }
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $classArr = Db::name("classification")->order("sort asc")->select();
            $this->assign("classArr",$classArr);
            return $this->fetch();
        }
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('产品名称不能为空');
            }
            if(empty($post['class_id'])){
                $this->error('分类不能为空');
            }
            if(empty($post['url'])){
                $this->error('跳转链接不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'img'=>$post['img'],
                'url'=>$post['url'],
                'class_id'=>$post['class_id'],
                'remarks'=>$post['remarks']
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
               $gid = $post['id'];
               //生成二维码
              // $res = $this->generate_qrcode($gid,$data["url"]); $data["url"]
               $res = $this->generate_qrcode($gid,"https://gzhshop.clkejj.com/h5/#/pages/webview/webview?url=".$data["url"]."&gid=".$gid);

               if($res["code"] == 1){
                    Db::name("goods")->where("id",$gid)->update(["qrcode"=>$res["data"]]);
               }
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $post = $this->request->param();
            $row = $this->model->where('id',$post['id'])->find();
            $this->assign('row', $row); 
            $classArr = Db::name("classification")->order("sort asc")->select();
            $this->assign("classArr",$classArr);
            return $this->fetch();
        }
       
    }

    //生成H5二维码
    public function generate_qrcode($gid,$url){
        $code = new QRcode();
        $uploadPath = public_path("uploads/" . date('Ymd'));
        if (!is_dir($uploadPath)) {
            mkdir($uploadPath, 0777, true);
        }
        $code->png($url,"uploads/".date('Ymd').'/'.time().'.png',$gid)//生成二维码
        ->getPath();//获取二维码生成的地址(二维码的相对路径)
        return [
            "code"=>1,
            "data"=>$code ->entry()
        ];
    }

    

    
}